<?php
header('content-type:text/html;charset=utf-8');
echo '<pre>';
//捕获和非捕获的例子
$subject='2016-05-01';
$pattern='#(\d{4})-(\d{2})-(\d{2})#';
preg_match($pattern, $subject,$matches);
print_r($matches);
// $replacement='\2-\3-\1';
$replacement='$2-$3-$1';
echo preg_replace($pattern, $replacement, $subject);
echo '<hr/>';
//使用命名捕获分组的形式，可以根据起的名字进行调用
$subject='2016-05-01';
$pattern='#(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})#';
preg_match($pattern, $subject,$matches);
print_r($matches);
echo '<hr/>';
$subject='000000';
$pattern='#(?<n>0{3})#';
$pattern='#(?<n>0{3})\k<n>#';
$pattern="#(?<n>0{3})\k'n'#";
$pattern='#(?<n>0{3})\g{n}#';
$pattern='#(?<n>0{3})\k{n}#';
preg_match($pattern, $subject,$matches);
print_r($matches);
//另外一种形式的命名分组
$pattern='#(?P<name>0{3})(?P=name)#';
preg_match($pattern, $subject,$matches);
print_r($matches);
echo '<hr/>';
//[url]1.jpg[/url] [url]2.jpg[/url]
$subject='[url]1.jpg[/url][url]2.jpg[/url]';
echo preg_replace('#\[url\](\d+\.jpg)\[/url\]#', '<img src="$1" alt=""/>', $subject);
//非捕获分组,?:
echo '<hr/>';
$subject='hello king';
$pattern='#hello (?:world|king|maizi)#i';
preg_match($pattern, $subject,$matches);
print_r($matches);
/*原子分组(?>)*/
echo '<hr/>';
$subject='hello king';
$pattern='#hello (?>world|king|maizi)#i';
preg_match($pattern, $subject,$matches);
print_r($matches);
echo '</pre>';





